package com.fxp.project.jiuselu.web.models.coupon.service.impl;

import com.fxp.project.jiuselu.commons.constant.ConstantCode;
import com.fxp.project.jiuselu.commons.exceptions.JiuLuException;
import com.fxp.project.jiuselu.web.models.coupon.entity.JslCoupon;
import com.fxp.project.jiuselu.web.models.coupon.entity.JslUserCoupon;
import com.fxp.project.jiuselu.web.models.coupon.mapper.JslCouponMapper;
import com.fxp.project.jiuselu.web.models.coupon.mapper.JslUserCouponMapper;
import com.fxp.project.jiuselu.web.models.coupon.qo.UserReceiveCouponQo;
import com.fxp.project.jiuselu.web.models.coupon.service.IJslUserCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxp.project.jiuselu.web.models.coupon.vo.JslUserCouponVo;
import com.fxp.project.jiuselu.web.models.coupon.vo.RegistCouponVo;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserMapper;
import io.netty.util.internal.ObjectUtil;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户优惠券记录表 服务实现类
 * </p>
 *
 * @author Jason.Wang
 * @since 2021-11-11
 */
@Service
public class JslUserCouponServiceImpl extends ServiceImpl<JslUserCouponMapper, JslUserCoupon> implements IJslUserCouponService {

    @Autowired
    JslUserCouponMapper jslUserCouponMapper;
    @Autowired
    private JslCouponMapper jslCouponMapper ;
    @Autowired
    private JslUserMapper jslUserMapper;

    /**
     * selectJslUserCouponVoForIndexShow: 查询当前用户的优惠券<br/>
     *
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/11 10:42
     */
    @Override
    public List<JslUserCouponVo> selectJslUserCouponVoForIndexShow(Long userId) {
        return jslUserCouponMapper.selectJslUserCouponVoForIndexShow(userId);
    }

    @Override
    public void selectJslUserCouponByOrderId(BigDecimal couponPrice,String orderId) {
        JslUserCoupon jslUserCoupon = jslUserCouponMapper.selectJslUserCouponVoByOrderId(orderId);
        if(jslUserCoupon!=null){
            if(jslUserCoupon.getIsSend()!=1 && couponPrice !=null && jslUserCoupon.getCategory() ==1){
                jslUserCoupon.setRemAmount(jslUserCoupon.getRemAmount().add(couponPrice));
                jslUserCoupon.setIsSend(1);
                jslUserCouponMapper.updateUserCoupon(jslUserCoupon);
            }
        }

    }

    /**
     * selectJslUserCouponVoList: 查询当前用户的优惠券<br/>
     *
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/11 15:21
     */
    @Override
    public List<JslUserCouponVo> selectJslUserCouponVoList(Long userId,LocalDateTime expireTime) {
        List<JslUserCouponVo> jslUserCouponVos = jslUserCouponMapper.selectJslUserCouponVoList(userId, expireTime);
        for(JslUserCouponVo coupon : jslUserCouponVos){
            if(coupon.getRemAmount().compareTo(new BigDecimal("0.00"))==0){
                coupon.setRemAmount(coupon.getAmount());
            }
        }
        return jslUserCouponVos;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void receiveCoupon(UserReceiveCouponQo userReceiveCouponQo,Long userId) {
//        LocalDateTime nowDate = LocalDateTime.now();
//        JslCoupon jslCoupon = jslCouponMapper.selectById(userReceiveCouponQo.getCouponId());
//        if(null == jslCoupon){
//            throw new JiuLuException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前优惠券不存在");
//        }
//        jslCoupon.setSendNum(jslCoupon.getSendNum()+1);
//        jslCouponMapper.updateById(jslCoupon);
//        JslUserCoupon jslUserCoupon = new JslUserCoupon();
//        jslUserCoupon.setCouponId(String.valueOf(jslCoupon.getCouponId()));
//        jslUserCoupon.setUserId(userId);
//        jslUserCoupon.setStatus(0);
//        jslUserCoupon.setCreateTime(nowDate);
//        jslUserCoupon.setExpireTime(nowDate.plusDays(jslCoupon.getDays()));
//        jslUserCouponMapper.insert(jslUserCoupon);
        String coupon = userReceiveCouponQo.getCouponId();
        String substring = coupon.substring(0, coupon.length());
        String[] result = substring.split(",");
        for (String couponId : result) {
            LocalDateTime nowDate = LocalDateTime.now();
            JslCoupon jslCoupon = jslCouponMapper.selectById(couponId);
            if (null == jslCoupon) {
                throw new JiuLuException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前优惠券不存在");
            }
            for (long i = jslCoupon.getRegNum(); i > 0; i--) {
                jslCoupon.setSendNum(jslCoupon.getSendNum() + 1);
                jslCouponMapper.updateById(jslCoupon);
                JslUserCoupon jslUserCoupon = new JslUserCoupon();
                jslUserCoupon.setCouponId(String.valueOf(jslCoupon.getCouponId()));
                jslUserCoupon.setAmount(jslCoupon.getAmount());
                jslUserCoupon.setUserId(userId);
                jslUserCoupon.setStatus(0);
                jslUserCoupon.setCreateTime(nowDate);
                jslUserCoupon.setExpireTime(jslCoupon.getEndTime());
                jslUserCouponMapper.insert(jslUserCoupon);
            }
        }
    }


    @Override
    public List<RegistCouponVo> receiveRegistCoupon(Long userId) {
        LocalDateTime endTime = LocalDateTime.now();
        JslUser jslUser = jslUserMapper.selectNewUserByUserId(userId);
        List<JslUserCouponVo> jslUserCouponVos = jslUserCouponMapper.selectJslUserCouponVoForIndexShow(userId);
        List<JslUserCouponVo> jslUserCouponVos1 = jslUserCouponMapper.selectJslUserCategoryVoForIndexShow(userId);
        //查询当前用户有无优惠券,自动领取
        if(jslUser !=null && jslUserCouponVos.size()==0 ) {
            List<RegistCouponVo> registCouponVos = jslCouponMapper.receiveRegistCoupon(userId, endTime);
            if (null != registCouponVos ) {
                for (RegistCouponVo vo : registCouponVos) {
                    JslCoupon jslCoupon = jslCouponMapper.selectById(vo.getId());
                    //用户通过赠送数量领取多张优惠券
                    for (long i = jslCoupon.getRegNum(); i > 0; i--) {
                        jslCoupon.setSendNum(jslCoupon.getSendNum() + 1);
                        jslCouponMapper.updateById(jslCoupon);
                        JslUserCoupon jslUserCoupon = new JslUserCoupon();
                        jslUserCoupon.setCouponId(String.valueOf(jslCoupon.getCouponId()));
                        jslUserCoupon.setAmount(jslCoupon.getAmount());
                        jslUserCoupon.setUserId(userId);
                        jslUserCoupon.setStatus(0);
                        jslUserCoupon.setCreateTime(LocalDateTime.now());
                        jslUserCoupon.setExpireTime(jslCoupon.getEndTime());
                        jslUserCouponMapper.insert(jslUserCoupon);
                    }

                }
            }
        }
        if(jslUser !=null && jslUserCouponVos1.size()==0) {
            List<RegistCouponVo> registCouponVos = jslCouponMapper.receiveRegistCoupon(userId, endTime);
            if (null != registCouponVos ) {
                for (RegistCouponVo vo : registCouponVos) {
                    JslCoupon jslCoupon = jslCouponMapper.selectById(vo.getId());
                    //用户通过赠送数量领取多张优惠券
                    for (long i = jslCoupon.getRegNum(); i > 0; i--) {
                        jslCoupon.setSendNum(jslCoupon.getSendNum() + 1);
                        jslCouponMapper.updateById(jslCoupon);
                        JslUserCoupon jslUserCoupon = new JslUserCoupon();
                        jslUserCoupon.setCouponId(String.valueOf(jslCoupon.getCouponId()));
                        jslUserCoupon.setAmount(jslCoupon.getAmount());
                        jslUserCoupon.setUserId(userId);
                        jslUserCoupon.setStatus(0);
                        jslUserCoupon.setCreateTime(LocalDateTime.now());
                        jslUserCoupon.setExpireTime(jslCoupon.getEndTime());
                        jslUserCouponMapper.insert(jslUserCoupon);
                    }

                }
            }
        }
        return null;
    }

            //    return registCouponVos;

    //这里写法建议后期改一下
    @Override
    public List<RegistCouponVo> registCoupon(LocalDateTime endTime) {
        LocalDateTime now = LocalDateTime.now();
        List<RegistCouponVo> registCouponVos = jslCouponMapper.registCoupon(now);
        return registCouponVos;
    }

    @Override
    public int updateCouponStatus(String orderId) {
        return jslUserCouponMapper.updateCouponStatus(orderId);
    }
}


